//#include <stdafx.h>
#ifndef URG_HEADER
#define URG_HEADER

#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;
typedef struct
{
  enum {
    MODL = 0,                   //!< Sensor model information
    DMIN,                       //!< Minimum measurable distance [mm]
    DMAX,                       //!< Maximum measurable distance [mm]
    ARES,                       //!< Angle of resolution
    AMIN,                       //!< Minimum measurable area
    AMAX,                       //!< Maximum measurable area
    AFRT,                       //!< Front direction value
    SCAN,                       //!< Standard angular velocity
  };
  string model;                 //!< Obtained MODL information
  long distance_min;            //!< Obtained DMIN information
  long distance_max;            //!< Obtained DMAX information
  int area_total;               //!< Obtained ARES information
  int area_min;                 //!< Obtained AMIN information
  int area_max;                 //!< Obtained AMAX information
  int area_front;               //!< Obtained AFRT information
  int scan_rpm;                 //!< Obtained SCAN information

  int first;                    //!< Starting position of measurement
  int last;                     //!< End position of measurement
  int max_size;                 //!< Maximum size of data
  long last_timestamp;          //!< Time stamp when latest data is obtained
} urg_state_t;

enum {
  Timeout = 150,               // [msec]
  EachTimeout = 2,              // [msec]
  LineLength = 64 + 3 + 1 + 1 + 1 + 16,
};
void delay(int msec);
int com_changeBaudrate(long );
// Serial transceiver
int com_connect( char* , long );
void com_disconnect(void);
int com_send( char* , int );
int com_recv(char* , int , int );
// The command is transmitted to URG
int urg_sendTag(char* );
// Read one line data from URG
int urg_readLine(char *);
// Trasmit command to URG and wait for response
int urg_sendMessage( char* , int , int* );
// Change baudrate
int urg_changeBaudrate(long );
// Read out URG parameter
int urg_getParameters(urg_state_t* );
/*!
  \brief Connection to URG

  \param state [o] Sensor information
  \param port [i] Device
  \param baudrate [i] Baudrate [bps]

  \retval 0 Success
  \retval < 0 Error
*/
int urg_connect(urg_state_t* , char* ,  long );
/*!
  \brief Disconnection
*/
 void urg_disconnect(void);

/*!
  \brief Receive range data by using GD command

  \param state[i] Sensor information

  \retval 0 Success
  \retval < 0 Error
*/
int urg_captureByGD(urg_state_t*);
/*!
  \brief Get range data by using MD command

  \param state [i] Sensor information
  \param capture_times [i] capture times

  \retval 0 Success
  \retval < 0 Error
*/
int urg_captureByMD(urg_state_t*, int );
// Decode 6bit data
long urg_decode(char , int );
// Receive range data
int urg_addRecvData(char *, long *, int* * );
int checkSum(char , int , char );
int urg_receiveData(urg_state_t* , long*, size_t );
void outputData(long *,int , int ,double );
void Output3DData(double *,int ,int ,double);

 //!  Finds the serial port
extern int urg_serial_find_port(void);

//!  Returns the name of the serial port found
extern const char *urg_serial_port_name(int index);

/*!
  \brief Checks whether the serial port corresponds to a URG or not

  \retval 1 It is a URG
  \retval 0 Unknown
  \retval <0 Error
*/
extern int urg_serial_is_urg_port(int index);
#endif